<template>
    <base-breadcrumb>
        <searchData :searchData="searchArr" @search="search"></searchData>
        <a-card>
            <div class="table-operator">
                <a-button v-btnPermission="'bm_019_add_01'" type="primary" icon="plus" @click="handleUpdate('add', {})"
                    >新增</a-button
                >
            </div>
            <list-table
                ref="table"
                size="default"
                rowKey="id"
                :columns="columns"
                :request="loadData"
                :alert="true"
                show-size-changer
                show-quick-jumper
                showPagination="auto"
            >
                <span slot="description" slot-scope="text">
                    <ellipsis :length="8" tooltip>{{ text }}</ellipsis>
                </span>
                <span slot="operation" slot-scope="text, record">
                    <MoreButton>
                    <a v-btnPermission="'bm_019_loo_04'" @click="handleUpdate('look', record)">查看</a>
                    <span
                        v-btnPermission="'bm_019_edi_02'"
                        v-if="record.status == 'DRAFT' || record.status == 'REJECTED'"
                    >
                        <a @click="handleUpdate('edit', record)">编辑</a>
                    </span>
                    <span v-btnPermission="'bm_019_del_03'" v-if="record.status == 'DRAFT' || record.status == 'REJECTED'">
                        <a @click="handleUpdate('del', record)">删除</a>
                    </span>
                    <span v-if="record.status == 'SUBMITTED'&&hideBuquBtn()">
                        <a @click="handleUpdate('cancel', record)">撤销</a>
                    </span>
                    <span v-btnPermission="'bm_019_tov_05'" v-if="record.status == 'PAST'">
                        <a @click="handleUpdate('abolish', record)">作废</a>
                    </span>
                        <!-- v-btnPermission="'bm_019_moi_05'" -->
                    <span v-if="
                      record.status === 'PAST' || record.status === 'SUBMITTED' || record.status === 'REJECTED' || record.status === 'ABOLISH'
                    ">
                        <approval-button :status="record.status" :id="record.id"></approval-button>
                    </span>
                    <span v-btnPermission="'bm_019_ltb_06'" v-if="record.status == 'PAST' && canbefill(record)">
                        <a @click="handleUpdate('fill', record)">填报</a>
                    </span>
                        </MoreButton>
                </span>
                <span slot="status" slot-scope="text, record">
                    <list-status
                        :status="record.status"
                        :isApproval="true"
                        conversion="en"
                        :text="text"
                    ></list-status>
                </span>
            </list-table>
        </a-card>
    </base-breadcrumb>
</template>

<script>
import { getSubcontractorCost, delSubcontractorCost } from '@/api/businessAffairs/subcontractCostPrice'
import { quarterlyList, getSubcontractorCostList, subcontractorCostDel } from '@/api/businessAffairs/subcostprice'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectArea from '@/components/Select/SelectArea' // 区域
import SelectFormat from '@/components/Select/SelectFormat' // 业态
import moment from 'moment'

const columns = [
    {
        title: '所属分公司',
        ellipsis: true,
        dataIndex: 'branchCompanyName',
    },
    {
        title: '区域',
        ellipsis: true,
        dataIndex: 'zoneName',
    },
    {
        title: '业态',
        ellipsis: true,
        dataIndex: 'businessStatusName',
    },
    {
        title: '季度',
        dataIndex: 'quarterly',
    },
    {
        title: '填报人',
        ellipsis: true,
        dataIndex: 'creatorName',
    },
    {
        title: '填报时间',
        dataIndex: 'createTime',
    },
    {
        title: '状态',
        dataIndex: 'status',
        scopedSlots: { customRender: 'status' },
    },
    {
        title: '操作',
        dataIndex: 'operation',
        width: '230px',
        scopedSlots: { customRender: 'operation' },
    },
]
import { typeListApproval } from '@/utils/util'
import { STable } from '@/components'
export default {
    name:'a'+Date.now(),
    components: {
        STable,
    },
    data() {
        this.columns = columns
        return {
            queryParam: {},
            quarterlyDataList: [],
            loadData: async (parameter) => {
                const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
                // const [err, res] = await getSubcontractorCost(requestParameters)
                const res = await getSubcontractorCostList(requestParameters)
                return new Promise((resolve, reject) => {
                    if (!res) {
                        reject({data: []})
                    } else {
                        resolve(res)
                    }
                })
            },
        }
    },
    computed: {
      searchArr() {
        return [
          {
              name: '分公司',
              valueKey: 'branchComId',
              type: SelectSubCompany,
          },
          {
              name: '区域',
              keyValue: 'branchComId',
              valueKey: 'zoneId',
              type: SelectArea,
          },
          {
              name: '业态',
              valueKey: 'businessStatusId',
              type: SelectFormat,
          },
          {
              name: '季度',
              value: 'quarterlyId',
              type: 'select',
              data: this.quarterlyDataList
          },
          {
              name: '填报人',
              value: 'creatorName',
              type: 'input',
          },
          {
              name: '填报时间',
              value: 'startEndDate',
              type: 'dateRange',
          },
          {
              name: '状态',
              value: 'auditStatus',
              type: 'select',
              data: typeListApproval
          },
        ]
      },
      canbefill() {
        return (record) => {
          let filled = record.orgProjectIds
          return this.$currentPost().code == 'PD' && !filled.includes(this.$currentPost().id)
        }
      }
    },
    mounted() {
      this.quarterlyList()
    },
    methods: {
        quarterlyList() {
          quarterlyList().then((res) => {
            this.quarterlyDataList = res.data?.map((v) => {
              return { label: v.name, value: v.id }
            })
          })
        },
        //搜索
        search(val) {
            this.queryParam = {
                ...val,
                createTimeBegin:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[0].format('YYYY-MM-DD')) ||
                    void 0,
                createTimeEnd:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[1].format('YYYY-MM-DD')) ||
                    void 0,
            }
            delete this.queryParam.startEndDate
            this.$refs.table.refresh(true)
        },
        async delSubcontractorCost(data) {
            const [err, res] = await delSubcontractorCost({ id: data.id })
            if (!err) {
                this.$message.success('成功')
                this.$refs.table.refresh(true)
            }
        },
        //新增
        handleUpdate(type, data = {}) {
            if (type == 'del' || type == 'cancel' || type == 'abolish') {
                let that = this
                let tmap = {
                  del: {id: 1, name: '删除'},
                  abolish: {id: 2, name: '作废'},
                  cancel: {id: 3, name: '撤销'},
                }
                let param = {
                  id: data.id,
                  type: tmap[type].id
                }
                this.$confirm({
                    title: '温馨提示',
                    content: `确定是否${tmap[type].name}数据？`,
                    okText: '确认',
                    okType: 'danger',
                    cancelText: '取消',
                    onOk: () => {
                        return subcontractorCostDel(param).then((res) => {
                          if (res.code == 200 || res.data.code == 200) {
                            this.$message.success('成功')
                            this.$refs.table.refresh(true)
                          }
                        })
                    },
                })
            } else {
                this.$router.push({
                    name: 'updateSubcontractCostPrice',
                    query: {
                        id: data.id || '',
                        pageType: type,
                    },
                })
            }
        },
    },
}
</script>


